<template>
  <div>
<!-- 面包屑部分 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCate">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table :data="cateList" :columns="columns" border :show-row-hover="false" show-index :expand-type="false" :selection-type="false" index-text="#">
        <!-- 是否有效模板 -->
        <template slot="isEff" scope="scope">
          <i v-if="!scope.row.cat_deleted" style="color: lightgreen" class="el-icon-success"></i>
          <i v-else style="color: red" class="el-icon-error"></i>
      </template>
      <template slot="sort" scope="scope">
        <el-tag v-if="scope.row.cat_level === 0" type="success">一级</el-tag>
        <el-tag v-else-if="scope.row.cat_level === 1" type="primary">二级</el-tag>
        <el-tag v-else type="warning">三级</el-tag>
      </template>
      <template slot="opera" scope="scope">
        <div>
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="editCate(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="delCate(scope.row)">删除</el-button>
        </div>
      </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="cateQueryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="cateQueryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="cateDialogVisible"
      width="50%"
      @close="handleClose">
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
          clearable
          v-model="selectParId"
            :options="parentCateList"
            :props="addProps"
            @change="parentIdChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateBtn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      cateList: [],
      // 查询参数
      cateQueryInfo: {
        pagenum: 1,
        pagesize: 10
      },
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isEff'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opera'
        }
      ],
      total: 0,
      // 添加分类对话框的显示与隐藏
      cateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 级联选择器配置项
      addProps: {
        expandTrigger: 'hover',
        children: 'children',
        label: 'cat_name',
        value: 'cat_id',
        checkStrictly: true
      },
      parentCateList: [],
      // 级联选择器选中的父级分类id数组
      selectParId: []
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.cateQueryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取分类列表失败')
      this.cateList = res.data.result
      this.total = res.data.total
      console.log(res)
    },
    editCate () {},
    delCate () {},
    handleSizeChange () {},
    handleCurrentChange () {},
    async addCate () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取父级分类列表失败')
      this.parentCateList = res.data
      console.log(res)
      this.cateDialogVisible = true
    },
    // 添加分类对话框的关闭事件
    handleClose () {
      this.$refs.addCateFormRef.resetFields()
      this.selectParId = []
      this.addCateForm = {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      }
    },
    // 级联选择器的change事件
    parentIdChange () {
      if (this.selectParId.length > 0) {
        this.addCateForm.cat_pid = this.selectParId[this.selectParId.length - 1]
        this.addCateForm.cat_level = this.selectParId.length
      } else {
        this.addCateForm.cat_level = 0
        this.addCateForm.cat_pid = 0
      }
    },
    // 添加对话框的提交事件
    addCateBtn () {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return this.$message.error('请输入正确的分类信息')
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        console.log(res)
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        this.$message.success('添加分类成功')
        this.getCateList()
        this.cateDialogVisible = false
      })
    }
  }
}
</script>

<style scoped lang="less">
.el-row {
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
.el-cascader-panel .el-radio {
     display: none!important;
   }

</style>
